package com.hollycrm.hollysqm.demo;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.io.FileUtils;

import com.hollycrm.hollysqm.core.voice.bean.ParseResult;
import com.hollycrm.hollysqm.core.voice.parsing.IVoiceParse;
import com.hollycrm.hollysqm.core.voice.parsing.factory.VoiceParserFactory;
import com.hollycrm.hollysqm.core.waveform.util.AudioUtil;

/**
 * 音频转换测试
 * @author jianglong
 * @date 2017年4月20日 下午3:21:56
 */
public class WaveformMain {

	public static void main(String[] args) throws Exception {
		String mp3File= "D:\\temp\\hollysqm\\voc\\2ECBED29B2AF453B9A51CE43DC63A323.mp3";
		String wavFile= "D:\\temp\\hollysqm\\voc\\2ECBED29B2AF453B9A51CE43DC63A323.wav";
		String xmlFile = "D:\\temp\\hollysqm\\voc\\2ECBED29B2AF453B9A51CE43DC63A323.xml";
		String imgFile = "D:\\temp\\hollysqm\\voc\\2ECBED29B2AF453B9A51CE43DC63A323.jpg";
		//生成wav文件和img文字图片
		AudioUtil.createWavAndImage(mp3File, wavFile, xmlFile, imgFile);
		
//		AudioUtil.covertMp3ToWav(mp3File,wavFile);		
//		System.out.println("xml文件解析，xmlFile = " + xmlFile);
//        //xml解析
//        IVoiceParse parse = VoiceParserFactory.getInstance().defaultParser(new FileInputStream(new File(xmlFile)), true);
//        ParseResult pr = parse.parse();
//
//        //图形绘制
//        int imgW = (int) pr.getDuration() / 10;
//        double k = 1D, ration = 1D;
//        while (imgW > 60000) {
//            ration = k * (k + 0.1);
//            k = k + 0.1;
//            imgW = (int) Math.ceil(imgW / k);
//        }
//        System.out.println("绘制波形图，imgWidth = " + imgW + ", imgHeight = 220");
//        AudioUtil.drawWavImage(wavFile, imgFile, imgW, 200); //绘制波形图
//        System.out.println("重绘制波形图，加入文字对话信息");
//        AudioUtil.repaintWavImage(imgFile, pr.getWordTimes(), null, ration); // 重绘，添加文字信息
//        //删除无用文件
//        System.out.println("删除临时文件wav, xml");
//        //FileUtils.delete(wavFile,mp3File, xmlFile);
//        FileUtils.deleteQuietly(new File(wavFile));
//        FileUtils.deleteQuietly(new File(xmlFile));
//        System.out.println("波形图创建结束");			
	}
		
}
